package com.monitor.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.monitor.entity.ErpParameters;
import com.monitor.service.ErpParametersService;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 轮胎参数表 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-12
 */
@RestController
@RequestMapping("/erpParameters")
@Api(description="轮胎参数")
public class ErpParametersController {

    @Resource
    private ErpParametersService parametersService;


    @RequestMapping(value = "/inertOrUpdateParameters",produces="text/html;charset=utf-8")
    @ApiOperation("添加Or修改 轮胎参数")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<ErpParameters> inertOrUpdateParameters(ErpParameters parameters) {
        Result<ErpParameters>  result=new Result<ErpParameters>();
        try{
            parametersService.insertOrUpdate(parameters);
            result.setCount(1);
            result.setData(parameters);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getParameterByParameterId")
    @ApiOperation("根据参数id获取参数信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "parameterId", value = "参数ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<ErpParameters> getParameterByParameterId(Integer parameterId) {
        Result<ErpParameters> result=new Result<ErpParameters>();
        try{
            ErpParameters parameters=parametersService.selectById(parameterId);
            result.setCount(1);
            result.setData(parameters);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/removeParameters")
    @ApiOperation("根据参数id删除多个参数信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "parameterIds", value = "参数IDs", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> removeParameter(String parameterIds) {
        Result<Integer> result=new Result<Integer>();
        try{
            result.setCount(1);
            result.setData(parametersService.delete(new EntityWrapper<ErpParameters>().in("id",parameterIds))?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getParameterPage")
    @ApiOperation("根据查询参数分页")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),*/
            @ApiImplicitParam(name = "norms", value = "规格", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "model", value = "型号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "arand", value = "品牌", required = false, dataType = "String", paramType = "query")
    })
    public @ResponseBody
    Result<Page<ErpParameters>> getParameterPage(@RequestParam(value = "page", defaultValue = "1") int page,
                                              @RequestParam(value="limit",defaultValue="10") int limit, String norms,String model,String arand) {
        Result<Page<ErpParameters>> result=new Result<Page<ErpParameters>>();
        try{
            Page<ErpParameters> page1=new Page<ErpParameters>(page,limit);
            Page<ErpParameters> carPage=new Page<ErpParameters>(page,limit);
            if(StringUtils.isEmpty(norms)&&StringUtils.isEmpty(model)&&StringUtils.isEmpty(arand)){
                carPage=parametersService.selectPage(page1,new EntityWrapper<ErpParameters>());
            }
            if(!StringUtils.isEmpty(norms)){
                carPage=parametersService.selectPage(page1,new EntityWrapper<ErpParameters>().eq("norms",norms));
            }
            if(!StringUtils.isEmpty(model)){
                carPage=parametersService.selectPage(page1,new EntityWrapper<ErpParameters>().eq("model",model));
            }
            if(!StringUtils.isEmpty(arand)){
                carPage=parametersService.selectPage(page1,new EntityWrapper<ErpParameters>().eq("arand",arand));
            }
            //Page<ErpCustomerCar> page=new Page<ErpCustomerCar>(1,2);
            result.setCount(carPage.getRecords().size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

}

